package miku.firstgame.ui.card
{
	import com.saia.starlingPunk.SPEngine;
	import com.saia.starlingPunk.SPEntity;

	import feathers.controls.Label;

	import flash.text.TextFormat;

	import miku.firstgame.model.gold.card.Card;
	import miku.firstgame.model.gold.interfaces.ICard;

	import starling.display.Quad;
	import starling.events.Event;

	public class CardUI extends SPEntity
	{
		public var card:ICard

		protected var back:Quad;

		public static const WIDTH:Number = 48;
		public static const HEIGHT:Number = 66;

		private var _isMoved:Boolean;

		public function CardUI(card:ICard)
		{
			super(0, 0, "card");
			this.card = card;
			addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
		}

//		override public function added():void
//		{
//			super.added();
//			back = new Quad(WIDTH, HEIGHT, 0xcff765);
//			addChild(back);
//			setupGraphic();
//		}

		public function get isMoved():Boolean
		{
			return _isMoved;
		}

		public function set isMoved(value:Boolean):void
		{
			_isMoved = value;
		}

		private function onAddToStage(e:Event):void
		{
			back = new Quad(WIDTH, HEIGHT, 0x0f0705);
			addChildAt(back, 0);
			setupGraphic();
		}

		protected function setupGraphic():void
		{
			//var textureAtlas:TextureAtlas = new TextureAtlas(Texture.fromBitmap(new ExampleAssets.ATLAS_TEXTURE()), XML(new ExampleAssets.ATLAS_DATA()));
//			var image:Image = new Image(Texture.fromBitmap(new ui())); //textureAtlas.getTexture("burger"));
//			addChild(image);

			var label:Label = new Label();
			label.text = card.name;
			//label.textRendererProperties.textFormat = new TextFormat("Microsoft YaHei,微软雅黑,tahoma,Arial,SimSun", 16, 0x0);
			addChild(label);
			label.validate();
			label.x = WIDTH - label.width >> 1;
			label.y = HEIGHT - label.height >> 1;
			if (!card.name)
			{
				trace("cardName is null");
			}
		}

		override public function removed():void
		{
			removeChildren(0, -1, true);
			super.removed();
		}

		public override function dispose():void
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			super.dispose();
		}

		override public function update():void
		{
			super.update();
			if (isMoved)
			{
				doCollide();
			}
		}

		/**
		 * 做卡片碰撞的相关操作
		 * */
		protected function doCollide():void
		{

		}
	}
}
